package com.mybed.crm.controller;

import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.dto.CluedDto;
import com.mybed.crm.pojo.dto.CluesDto;
import com.mybed.crm.pojo.dto.GainDto;
import com.mybed.crm.pojo.pojo.Clues;
import com.mybed.crm.pojo.pojo.CluesPool;
import com.mybed.crm.pojo.pojo.CluesRecord;
import com.mybed.crm.pojo.pojo.Course;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.ProfileResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.CluesService;
import com.mybed.crm.utils.alioss.AliOssUtil;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/clues")
@Slf4j
public class CluesController {
    //线索管理 by lr
    @Autowired
    private CluesService cluesService;


    /**
     * 线索分页查询
     * @param cluesDto
     * @return
     */
    @GetMapping("/clue/list")
    public PageResult pageCluseQuery(CluesDto cluesDto,
                                     @RequestParam("params[beginCreateTime]") String begin,
                                     @RequestParam("params[endCreateTime]") String end ){

        cluesDto.setBeginCreateTime(begin);
        cluesDto.setEndCreateTime(end);
        log.info("传入的参数为：{}",cluesDto);
        PageResult pageResult = cluesService.cluesListQuery(cluesDto);
        return pageResult;
    }
    /**
     * 线索池分页查询寻
     * @param cluesDto
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/clue/pool")
    public PageResult pagePoolQuery(CluesDto cluesDto ,
                                    @RequestParam("params[beginCreateTime]") String begin,
                                    @RequestParam("params[endCreateTime]") String end ){
        cluesDto.setBeginCreateTime(begin);
        cluesDto.setEndCreateTime(end);

        log.info("传入的参数为：{}",cluesDto);
        PageResult pageResult = cluesService.cluesPoolQuery(cluesDto);
        return pageResult;
    }
    /**
     * 查看线索 或线索池详情
     * @return
     */
    @GetMapping("/clue/{id}")
    public Result<Clues> looklookCluse(@PathVariable Long id){
        log.info("要查看的id是：{}",id);
        Result<Clues> result = cluesService.lookClue(id);
        return result;
    }

    /**
     * 添加线索
     * @param clues
     * @return
     */
    @Log("新增线索")
    @PostMapping("/clue")
    public Result addClues (@RequestBody Clues clues){
        log.info("要添加的线索为：{}",clues);
        cluesService.addClues(clues);
        return Result.success();
    }

    /**
     *  放入线索池
     * @param cluesPool
     * @return
     */
    @Log("放入线索池")
    @PutMapping("/clue/false/{id}")
    public Result addCluesPool(@PathVariable Long id , @RequestBody CluesPool cluesPool){
        cluesPool.setCluesId(id);
        log.info("要添到线索池的线索id为：{}",cluesPool);
        cluesService.addCluesPool(cluesPool);
        return Result.success();
    }

    /**继续跟进，补充信息
     * @param
     * @return
     */
    @Log("继续跟进补充信息")
    @PostMapping("/record")
    public Result keepClues(@RequestBody CluedDto cluedDto){
        log.info("要添加的信息为：{}",cluedDto);
        cluesService.keepClues(cluedDto);
        return Result.success();
    }

    /**
     * 查询回显跟进历史 tb_clues_record
     * @param clueId
     * @return
     */
    @GetMapping("/record/list")
    public PageResult getCluedList(long clueId){
        log.info("要查看的历史信息的id为：{}",clueId);
        PageResult pageResult = cluesService.getCluedlist(clueId);
        return pageResult ;
    }

    /**转商机
     *
     * @param clueId
     * @return
     */
    @Log("线索转商机")
    @PutMapping("/clue/changeBusiness/{clueId}")
    public Result putToBusiness(@PathVariable Long clueId){
        log.info("要转商机的线索为：{}",clueId);
        cluesService.putToBusiness(clueId);
        return Result.success();
    }

    /**捞出线索池
     * @param gainDto
     * @return
     */
    @PutMapping("/clue/gain")
    public Result gainCluesPool(@RequestBody GainDto gainDto){
        log.info("要捞取的线索们为：{}",gainDto.getIds());
        cluesService.gainCluesPool(gainDto);
        return Result.success();
    }

    /**线索分配
     *
     * @param gainDto
     * @return
     */
    @PutMapping("/clue/assignment")
    public Result CluesAssignment(@RequestParam("file") GainDto gainDto){
        log.info("要分配的id们是：{}",gainDto.getIds());
        cluesService.cluesAssignment(gainDto);
        return Result.success("分配成功");
    }

    /**
     * 批量添加
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/clue/importData")
    public ProfileResult<String> impoertDatas(MultipartFile file) {
        log.info("要上传的文件为：{}", file);
        String fileName = file.getOriginalFilename();
        // 获取文件内容类型
        String contentType = file.getContentType();
        // 获取文件大小
        long fileSize = file.getSize();

        // 打印文件信息
        System.out.println("文件名: " + fileName);
        System.out.println("内容类型: " + contentType);
        System.out.println("文件大小: " + fileSize + " 字节");
        return null;
    }


}
